
from numpy import median
from random import shuffle


starting_food = 0.0

n_hunts_this_round = 0
n_hunts_this_round_percent = 0.0
n_player = 0


def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
    
    global n_hunts_this_round_percent
    global n_hunts_this_round
    global n_player
    
    n_player = len(player_reputations)
    
    if round_number == 1: n_hunts_this_round = n_player
    
    
    #### add survival mode
    
    global starting_food
    if round_number == 1: starting_food = float(current_food)
    
    food_percent = float(current_food) / starting_food
    
    bias = (food_percent**0.2 * -1.2 + 1.0) * 4.5
    if bias > 1.0: bias = 1.0 
    elif bias < 0.0:  bias = 0.0 
    
    if round_number != 1: 
        n_hunts_this_round_percent = n_hunts_this_round_percent * (1.0 - bias) + 0.0 * (bias)
        n_hunts_this_round = int(round(n_hunts_this_round_percent * float(n_player),0))
    
    ####
    
    
    hunt_decisions = ['h'] * n_hunts_this_round
    hunt_decisions.extend(['s'] * (n_player - n_hunts_this_round))
    
    hunt_decisions = hunt_decisions[0:n_player]
    shuffle(hunt_decisions)
    
    return hunt_decisions;
    




def hunt_outcomes(food_earnings):
    # hunt_outcomes is called after all hunts for the round are complete.
    
    # Add any code you wish to modify your variables based on the outcome of the last round.
    
    # The variable passed in to hunt_outcomes for your use is:
    #     food_earnings: list of integers, the amount of food earned from the last round's hunts.
    #                    The entries can be negative as it is possible to lose food from a hunt.
    #                    The amount of food you have for the next round will be current_food
    #                    + sum of all entries of food_earnings + award from round_end.
    #                    The list will be in the same order as the decisions you made in that round.
    
    pass 
    # pass is a python placeholder for if you want to define a function that doesn't have
    # any other code. You should replace pass with your own code if you want to use this
    # function, otherwise leave it to prevent errors caused by an empty function.



def round_end(award, m, number_hunters):
    
    global n_hunts_this_round
    global n_hunts_this_round_percent
    global n_player
    
    number_hunters = number_hunters - n_hunts_this_round
    
    n_hunts_this_round = number_hunters / n_player
    
    n_hunts_this_round_percent = number_hunters / float(n_player) / float(n_player)
    
    # round_end is called after all hunts for the round are complete.
    
    # award - the total amount of food you received due to cooperation in the round.
    # Can be zero if the threshold m was not reached.
    
    # Add any code you wish to modify your variables based on the cooperation that occurred in
    # the last round.
    
    # The variables passed in to round_end for your use are:
    #     award: integer, total food bonus (can be zero) you received due to players cooperating
    #            during the last round. The amount of food you have for the next round will be
    #            current_food (including food_earnings from hunt_outcomes this round) + award.
    #     number_hunters: integer, number of times players chose to hunt in the last round.
    
    


